package cn.geekhalo.auth.web.endpoints;

import cn.geekhalo.auth.domain.SysUser;
import cn.geekhalo.auth.domain.vo.SysUserVo;
import cn.geekhalo.auth.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping(value = "v1/sys_user")
@Api(tags = "SysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping(value = "get_all_users")
    public void  getAllUser(){

    }

    @ApiOperation(value = "findById", notes = "根据id查询用户")
    @GetMapping(value = "find_by_id/{id}")
    public ResponseEntity<SysUserVo> findById(@PathVariable Long id){
        SysUser sysUser = sysUserService.findById(id).orElse(null);
        if(!Objects.isNull(sysUser)){
            return ResponseEntity.ok(new SysUserVo(sysUser));
        }else {
            return ResponseEntity.ok(null);
        }
    }

    @PostMapping(value = "save_user")
    public ResponseEntity<Boolean> saveUser(@RequestBody SaveReq req){
        sysUserService.createUser(req.getEmail(),req.getUsername());
        return ResponseEntity.ok(true);
    }

    @NoArgsConstructor
    @Data
    static class SaveReq{
        private String email;
        private String username;
    }
}
